#include "aed_physicscomp.h"

AED_PhysicsComp::AED_PhysicsComp()
{
    m_enabled=false;

    m_friction=1;
    m_restitution=0;
    m_sensor=false;
    m_shape="box"; 	//circle/box/polygon
    m_shape_size_x=1;
    m_shape_size_y=1;
    m_linear_damping=0;
    m_angular_damping=0;
    m_type="dynamic"; 	//static/dynamic
    m_fixed_rotation=false;
    m_bullet=false;
    m_density=1;

}


AED_PhysicsComp& AED_PhysicsComp::operator =(const AED_PhysicsComp &comp){
    m_enabled = comp.m_enabled;
    m_friction = comp.m_friction;
    m_restitution = comp.m_restitution;
    m_sensor = comp.m_sensor;
    m_shape = comp.m_shape;
    m_shape_size_x=comp.m_shape_size_x;
    m_shape_size_y=comp.m_shape_size_y;
    m_linear_damping = comp.m_linear_damping;
    m_angular_damping = comp.m_angular_damping;
    m_type = comp.m_type;
    m_fixed_rotation = comp.m_fixed_rotation;
    m_bullet = comp.m_bullet;
    m_points = comp.m_points;
    m_conds = comp.m_conds;
    m_density = comp.m_density;

    return *this;
}

void AED_PhysicsComp::AddCondition(const QString &name, const QString &attr, int omega, bool inv){
    Condition cond(name,attr,omega,inv);

    m_conds.append(cond);
}

void AED_PhysicsComp::RemoveCondition(int index){
    m_conds.removeOne(m_conds[index]);
}

QList<AED_PhysicsComp::Condition>* AED_PhysicsComp::GetConditionList(){
    return &m_conds;
}

bool AED_PhysicsComp::IsEnabled(){
    return m_enabled;
}

void AED_PhysicsComp::SetEnabled(bool enabled){
    m_enabled = enabled;
}

void AED_PhysicsComp::Save(QTextStream &file){
    if(!m_enabled)
        return;

    file << "   <component = Physics>" << endl;
    file << "      <friction = " << m_friction << ">" << endl;
    file << "      <restitution = " << m_restitution << ">" << endl;
    file << "      <sensor = " << m_sensor << ">" << endl;
    file << "      <shape = " << m_shape << ">" << endl;
    //file << "      <shape_size = " << m_shape_size << ">" << endl;
    file << "      <linear_damping = " << m_linear_damping << ">" << endl;
    file << "      <angular_damping = " << m_angular_damping << ">" << endl;
    file << "      <type = " << m_type << ">" << endl;
    file << "      <fixed_rotation = " << m_fixed_rotation << ">" << endl;
    file << "      <bullet = " << m_bullet << ">" << endl;
    if(m_shape=="polygon"){
        for(int i=0;i<m_points.size();i++){
            file << "      <point = " << m_points[i].x << " " << m_points[i].y << ">" << endl;
        }
    }

    for(int i=0;i<m_conds.size();i++){
        file << "      <condition>" << endl;
        file << "         <name = "<< m_conds[i].name << ">" << endl;
        file << "         <attribute = "<< m_conds[i].attr << ">" << endl;
        file << "         <omega = "<< m_conds[i].omega << ">" << endl;
        file << "         <inv = "<< m_conds[i].inv << ">" << endl;
        file << "      </condition>" << endl;
    }
    file << "   </component>" << endl;
}

void AED_PhysicsComp::Save(QDomDocument &doc, QDomElement &pelem){
    if(!m_enabled)
        return;
    QDomElement elem = doc.createElement("component");
    elem.setAttribute("comptype","Physics");
    elem.setAttribute("friction",m_friction);
    elem.setAttribute("restitution",m_restitution);
    elem.setAttribute("sensor",m_sensor);
    elem.setAttribute("shape",m_shape);
    elem.setAttribute("shape_size_x",m_shape_size_x);
    elem.setAttribute("shape_size_y",m_shape_size_y);
    elem.setAttribute("linear_damping",m_linear_damping);
    elem.setAttribute("angular_damping",m_angular_damping);
    elem.setAttribute("type",m_type);
    elem.setAttribute("fixed_rotation",m_fixed_rotation);
    elem.setAttribute("bullet",m_bullet);
    elem.setAttribute("density",m_density);
    if(m_shape=="polygon"){
        for(int i=0;i<m_points.size();i++){
            QDomElement pt = doc.createElement("point");
            pt.setAttribute("x",m_points[i].x);
            pt.setAttribute("y",m_points[i].y);
            elem.appendChild(pt);
        }
    }
    for(int i=0;i<m_conds.size();i++){
        QDomElement cond = doc.createElement("condition");
        cond.setAttribute("name", m_conds[i].name);
        cond.setAttribute("attribute", m_conds[i].attr);
        cond.setAttribute("omega", m_conds[i].omega);
        cond.setAttribute("inv", m_conds[i].inv);
        elem.appendChild(cond);
    }
    pelem.appendChild(elem);
}

void AED_PhysicsComp::Load(QDomElement &celem){
    m_enabled = true;
    QDomNodeList conds = celem.elementsByTagName("condition");
    m_friction = celem.attribute("friction").toFloat();
    m_restitution = celem.attribute("restitution").toFloat();
    m_sensor = celem.attribute("sensor").toInt();
    m_shape = celem.attribute("shape");
    float shape_size = celem.attribute("shape_size","-1").toFloat();
    if(shape_size==-1){
        m_shape_size_x = celem.attribute("shape_size_x","1").toFloat();
        m_shape_size_y = celem.attribute("shape_size_y","1").toFloat();
    }else{
        m_shape_size_x = shape_size;
        m_shape_size_y = shape_size;
    }

    m_linear_damping = celem.attribute("linear_damping").toFloat();
    m_angular_damping = celem.attribute("angular_damping").toFloat();
    m_type = celem.attribute("type");
    m_fixed_rotation = celem.attribute("fixed_rotation").toInt();
    m_bullet = celem.attribute("bullet").toInt();
    m_density = celem.attribute("density").toFloat();
    if(m_shape=="polygon"){
        QDomNodeList points = celem.elementsByTagName("point");
        for(int i=0;i<points.count();i++){
            QDomElement point = points.at(i).toElement();
            float x = point.attribute("x").toFloat();
            float y = point.attribute("y").toFloat();
            AddPoint(x,y);
        }

    }
    for(int i=0;i<conds.count();i++){
        QDomElement cond = conds.at(i).toElement();
        QString name = cond.attribute("name");
        QString attribute = cond.attribute("attribute");
        int omega = cond.attribute("omega").toInt();
        bool inv = cond.attribute("inv").toInt();
        AddCondition(name,attribute,omega,inv);
    }
}
